package com.XinYun.Library.modules.admin.bookCRUD.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONString;
import com.XinYun.Library.modules.admin.bookCRUD.dao.BooksDao;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookClassInfo;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookCntInfo;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookComparator;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowMsg;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowPrintMsg;
import com.XinYun.Library.modules.admin.common.AdminReturnCode;
import com.XinYun.Library.modules.admin.common.BasicDao;
import com.XinYun.Library.modules.admin.common.BasicService;
import com.XinYun.Library.modules.admin.common.JSON.AdminJSON;
import com.XinYun.Library.modules.admin.common.SqlMapper;
import com.XinYun.Library.modules.commons.BasicJson;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookService extends BasicService {

    private BooksDao booksDao = new BooksDao();

    public void getBookClassInfo(AdminJSON json, JSONObject jsonObj){
        //针对性解参
        String pageSize = jsonObj.get("pageSize", String.class);
        String pageIndex = jsonObj.get("pageIndex", String.class);
        String searchBy = jsonObj.get("searchBy", String.class);
        String keyWord = jsonObj.get("keyWord", String.class);
        //客制化校验格式
        if(StrUtil.hasEmpty(pageSize,pageIndex))
        {setMsgCode(json, AdminReturnCode.ERR_EMPTY_PARAM);}
        else if(!(Validator.isNumber(pageSize) && Validator.isNumber(pageIndex)))
        {setMsgCode(json, AdminReturnCode.ERR_WRONG_PARAM);}
        else {
            //无错解析为数字继续
            Integer pagesize = Integer.valueOf(pageSize);
            Integer pageindex = Integer.valueOf(pageIndex);
            //是否搜索处理
            String isSearch = null;
            //不为空
            if(!StrUtil.hasEmpty(searchBy,keyWord)){
                keyWord = fuzzy(keyWord);
                isSearch = " WHERE " +searchBy+ " LIKE " + "'"+keyWord+"'";
            }
            //获得书籍信息
            List<BookCntInfo> bcis = booksDao.getBookClassInfo(SqlMapper.GET_BOOK_INFO,pagesize,pageindex,isSearch);
            //获得数据量
            Long dataNum = booksDao.getDataNum(SqlMapper.GET_BOOK_INFO_NUM,isSearch);
            //结果判断
            if(bcis.isEmpty() || dataNum == 0)
            {setMsgCode(json, AdminReturnCode.ERR_REQUEST_OVERRIDE);}
            else {
                setMsgCode(json, AdminReturnCode.SUCCECSS);
                json.setData(bcis);
                json.setDataNum(dataNum);
            }
        }
    }

    /**
     * 添加新书
     * @return
     */
    public <T extends BasicJson> void addBook(T json,JSONObject jsonObj)  {
        //得参
        String num = jsonObj.getStr("num");
        String intro = jsonObj.getStr("intro");
        String bookname = jsonObj.getStr("bookname");
        String category = jsonObj.getStr("category");
        String url = jsonObj.getStr("url");
        //错误验证
        if (StrUtil.hasEmpty(bookname,intro,num,category))
            {setMsgCode(json, AdminReturnCode.ERR_EMPTY_PARAM);}
        else if(!Validator.isNumber(num))
            {setMsgCode(json, AdminReturnCode.ERR_WRONG_PARAM);}
        else {
            //验证无问题便添加
            BookClassInfo bci = new BookClassInfo(bookname,0,Integer.valueOf(num),intro,0, category,url);
            try {
                booksDao.addBookClass(List.of(bci));
                setMsgCode(json, AdminReturnCode.SUCCECSS);
            }catch (IllegalAccessException e){
                setMsgCode(json, AdminReturnCode.ERR_REFLECT_EXCEPTION);
            }
        }
    }

    /**
     * 修改书
     * @throws IllegalAccessException
     */
    public <T extends BasicJson> void alertBookInfo(T json, JSONObject jsonObj){
        String bookName = jsonObj.getStr("bookname");
        String newIntro = jsonObj.getStr("newIntro");
        String newNum = jsonObj.getStr("newNum");
        String newCategory = jsonObj.getStr("newCategory");
        //
        if(StrUtil.hasEmpty(newIntro,newNum,newCategory))
            {setMsgCode(json, AdminReturnCode.ERR_EMPTY_PARAM);}
        else if(!Validator.isNumber(newNum))
            {setMsgCode(json, AdminReturnCode.ERR_WRONG_PARAM);}
        else if(Integer.valueOf(newNum) < 0)
            {setMsgCode(json, AdminReturnCode.ERR_WRONG_PARAM);}
        else if(!isAlertable(bookName))
            {setMsgCode(json, AdminReturnCode.ERR_UNCHANGED);}
        else {
            try {
                booksDao.alterBookClassInfo(bookName,newIntro,Integer.valueOf(newNum),newCategory);
                setMsgCode(json, AdminReturnCode.SUCCECSS);
            } catch (IllegalAccessException e) {
                setMsgCode(json, AdminReturnCode.ERR_REFLECT_EXCEPTION);
            }
        }
    }

    public <T extends BasicJson> void deleteBook(T json,JSONObject jsonObj){
        String bookName = jsonObj.getStr("bookname");
        if(bookName.isEmpty())
        {setMsgCode(json, AdminReturnCode.ERR_EMPTY_PARAM);}
        else if(!isAlertable(bookName))
        {setMsgCode(json, AdminReturnCode.ERR_UNCHANGED);}
        else {
            booksDao.deleteBookClass(bookName);
            setMsgCode(json, AdminReturnCode.SUCCECSS);
        }
    }

    /**
     * 判断该书籍是否可修改
     * @return
     */
    private boolean isAlertable(String bookname){
        BookComparator compareData = booksDao.getCompareData(bookname);
        if(compareData.getSaved().equals(compareData.getTotal()))
            return true;
        return false;
    }

    public <T extends BasicJson> void getBookStateCount(T json){
        Map<String,Long> cntData = new HashMap<>();
        cntData.put("save",booksDao.getStateNum(1));
        cntData.put("loan",booksDao.getStateNum(0));
        cntData.put("out",booksDao.getStateNum(-1));
        if (cntData.isEmpty())
            {setMsgCode(json, AdminReturnCode.UNKNOWN_BOOK_STATE);}
        else {
            json.setData(cntData);
            setMsgCode(json, AdminReturnCode.SUCCECSS);
        }
    }
}
